package com.tensquare.user.service;

import com.tensquare.user.dao.UserDao;
import com.tensquare.user.pojo.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.print.DocFlavor;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by WF on 2020/10/23 10:32
 */
@Service
@Transactional
public class UserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private UserDao userDao;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private IdWorker idWorker;
    //1.获取验证码
    public void getCode(String mobile) {
        //1.1) 生成六位数的验证码
        int min = 100000;   //六位数的最小值
        int max = 999999;   //六位数的最大值
        Random random = new Random();
        int code = random.nextInt(max-min);  //得到一个[0,max-min)范围的随机数
        if(code < min){
            code = code + min ;
        }
        System.out.println("生成的六位数的验证码是：" + code);

        //1.2)将验证码放到redis中(存放5小时)
        redisTemplate.opsForValue().set(mobile,code,5, TimeUnit.HOURS);

        //1.3)向消费方发送消息，由其向阿里大于再发送消息
        //1.3.1)准备要发送的消息
        Map map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("code",code);
        map.put("templateCode","SMS_148593167");
        map.put("signName","品优购");
        //1.3.2)将消息发送出去
        rabbitTemplate.convertAndSend("sms",map);

        System.out.println("发送完成。。。");

    }

    //2.验证码是否正确
    public boolean isValidCode(String mobile, String code) {
        String validCode = (String) redisTemplate.opsForValue().get(mobile);
        return validCode.equals(code);

    }

    //3.添加用户
    public void add(User user) {
        //3.1）将密码加密后设置回去
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //3.2)设置主键
        user.setId(idWorker.nextId() + "");
        //3.3) 保存密码到数据库中
        userDao.save(user);
    }

    //4.用户登录
    public User login(User user) {
        //4.1)根据手机号查询用户
        User mobileUser = userDao.findByMobile(user.getMobile());
        //4.2)如果存在用户，就比对密码是否一致
        if(mobileUser != null){
            //参数1：输入的密码 参数2：数据库中的密码
            if(passwordEncoder.matches(user.getPassword(),mobileUser.getPassword())){
                return mobileUser;
            }
        }
        return null;
    }

    //5.删除用户
    public void delete(String userId) {
        userDao.deleteById(userId);
    }

    //6.增加粉丝数
    public void incfans(String friendid, int n) {
        userDao.incfans(friendid,n);
    }

    //7.增加关注数
    public void incfollow(String userid, int n) {
        userDao.incfollow(userid,n);
    }
}
